import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class ProductService {

  private products: Product[] = [
    new Product(1, "商品A", 300, 3.5, "这是第1个商品", ["电子产品", "硬件设备"]),
    new Product(2, "商品B", 105, 2, "这是第2个商品", ["电子产品"]),
    new Product(3, "商品C", 250, 1.5, "这是第3个商品", ["电子产品", "硬件设备"]),
    new Product(4, "商品D", 30, 4.5, "这是第4个商品", ["图书"]),
    new Product(5, "商品E", 199, 4.5, "这是第5个商品", ["图书"]),
    new Product(6, "商品F", 239, 4, "这是第6个商品", ["硬件设备"]),
    new Product(7, "商品G", 499, 5, "这是第7个商品", ["电子产品", "硬件设备"])
  ]

  private comments: Comment[] = [
    new Comment(1, 1, '2018-11-30 09:27', '小张', 3, '不错哦，好用'),
    new Comment(2, 1, '2018-11-30 19:27', '小A', 4, '棒棒哒，好用'),
    new Comment(3, 1, '2018-11-30 11:27', '小B', 5, '不错哦，好用'),
    new Comment(4, 2, '2018-10-30 11:27', '小B', 5, '不错哦，好用'),
  ]

  constructor() { }

  getProducts():Product[] {
    return this.products;
   }
  getProduct(id: number): Product {
    return this.products.find((product) => product.id == id);
  }
  getComments(id: number): Comment[] {
    return this.comments.filter((comment) => comment.productId == id);
  }
}

export class Product {
  constructor(
    public id: number,
    public title: string,
    public price: number,
    public rating: number,
    public desc: string,
    public categories: Array<string>
  ) {
  }
}

export class Comment {
  constructor(
    public id: number,
    public productId: number,
    public timestamp: string,
    public user: string,
    public rating: number,
    public desc: string) { }
}